﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AppManagement.Repositories;
using AppManagement.Models;

namespace AppManagement.Reports
{
    public partial class FrmPrintDoanhThu : Form
    {
        public DateTime NgayBatDau { get; set; }
        public DateTime NgayKetThuc { get; set; }

        public FrmPrintDoanhThu(DateTime _tuNgay, DateTime _denNgay)
        {
            InitializeComponent();
            this.NgayBatDau = _tuNgay;
            this.NgayKetThuc = _denNgay;
            LoadData();
        }

        public void LoadData()
        {
            long tongDoanhThu = 0;
            var tuyenXes = TuyenXeRepository.SelectAll();

            var dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("TuyenChuyenXe");
            dt.Columns.Add("NgayHoatDong");
            dt.Columns.Add("DonGia");
            dt.Columns.Add("SoVeBanDuoc");
            dt.Columns.Add("TongTien");

            for (int i = 0; i < tuyenXes.Count; i++)
            {
                dt.Rows.Add(
                            (i + 1).ToString(), 
                            tuyenXes[i].DiaDiem.TenDiaDiem + " - " + tuyenXes[i].DiaDiem1.TenDiaDiem, 
                            tuyenXes[i].NgayHoatDong,
                            string.Empty, 
                            string.Empty,
                            string.Empty);
                DataRow rowTuyenXe = dt.Rows[dt.Rows.Count - 1];

                var chuyenXes = ChuyenXeRepository.SelectByMaTuyenXe(tuyenXes[i].MaTuyenXe);
                long tongTien = 0;

                foreach (ChuyenXe chuyenxe in chuyenXes)
                {
                    int tongSoVe = 0;

                    var lichHoatDong = LichHoatDongRepository.SelectByMaChuyenXeAndRangeDate(chuyenxe.MaChuyenXe, this.NgayBatDau, this.NgayKetThuc);
                    if (lichHoatDong != null)
                    {
                        foreach (ChiTietLichHoatDong chitietLHD in lichHoatDong.ChiTietLichHoatDong)
                        {
                            tongSoVe += chitietLHD.Ve.Count;
                        }
                    }

                    dt.Rows.Add(
                                string.Empty,
                                "\t\t+ Giờ khởi hành - " + chuyenxe.GioKhoiHanh,
                                string.Empty,
                                String.Format("{0:#,##0;Nothing}", chuyenxe.DonGia),
                                tongSoVe,
                                String.Format("{0:#,##0;Nothing}", chuyenxe.DonGia * tongSoVe));
                    tongTien += (long)(chuyenxe.DonGia * tongSoVe);
                }
                rowTuyenXe[5] = String.Format("{0:#,##0;Nothing}", tongTien);
                tongDoanhThu += tongTien;
            }

            rptThongKeDoanhThu rpt = new rptThongKeDoanhThu();
            rpt.DataDefinition.FormulaFields["TuNgay"].Text = "'" + String.Format("{0:dd/MM/yyyy}", this.NgayBatDau) + "'";
            rpt.DataDefinition.FormulaFields["DenNgay"].Text = "'" + String.Format("{0:dd/MM/yyyy}", this.NgayKetThuc) + "'";
            rpt.DataDefinition.FormulaFields["DoanhThu"].Text = "'" + String.Format("{0:#,##0;Nothing}", tongDoanhThu) + " đồng'";
            rpt.DataDefinition.FormulaFields["NgayThangNam"].Text = "'Ngày " + DateTime.Now.Day.ToString() + " tháng " + DateTime.Now.Month.ToString() + " năm " + DateTime.Now.Year.ToString() + "'";
            rpt.SetDataSource(dt);
            crystalReportViewer1.ReportSource = rpt;
        }
    }
}
